<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>统计全 1 子矩形</title>
</head>
<body>
  <script>
    function fn(mat) {
      let rows = mat.length;
      let columns = mat[0].length;
      let c = 0;
      for (let i = 0; i < rows; i++) {
        for (let j = 0; j < columns; j++) {
          c += numSubmat1(mat, i, j);
        }
      }
      return c
    }
    var numSubmat1 = function (mat, rowIndex, columnIndex) {
      let rows = mat.length;
      let columns = mat[0].length;
      let c = 0;
      for (let i = rowIndex; i < rows; i++) {
        if (mat[i][columnIndex] === 0)
          break;
        for (let j = columnIndex; j < columns; j++) {
          if (mat[i][j] === 1) {
            c++;
          }
          else {
            columns = j
            break;
          }
        }
      }
      return c;
    }
    console.log(fn([[1, 0, 1], [1, 1, 0], [1, 1, 0]]))
  </script>
</body>
</html>